package com.garen.fire.web;

import com.garen.fire.biz.IFireCaseInfoBiz;
import com.garen.fire.biz.IFireCaseRecordBiz;
import com.garen.fire.biz.IFireDutyBiz;
import com.garen.fire.entity.FireCar;
import com.garen.fire.entity.FireCaseRecord;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.garen.common.*;
import org.springframework.web.servlet.ModelAndView;

import com.garen.sys.filter.SysFilter;
import com.garen.utils.DateUtils;


@Controller
@RequestMapping("/fire")
public final class FireCaseRecordAction extends BaseAction{

	@Autowired
	private IFireCaseRecordBiz iFireCaseRecordBiz;

	@Autowired
	private IFireDutyBiz iFireDutyBiz;
	
	@Autowired
	private IFireCaseInfoBiz iFireCaseInfoBiz;
	
	@RequestMapping("/firecaserecord/json1") 
	public ModelAndView json1(PageBean<FireCaseRecord> pb,FireCaseRecord fireCaseRecord) {
		iFireCaseRecordBiz.getFireCaseRecordList(pb,fireCaseRecord);
		return Json(pb);
	}

	//获取排版日期
	private Date[] getDutyDate(){
		Date kssj = null,jssj = null;
		int startClock = 8;//8点交接班
		Calendar cale = Calendar.getInstance();
		int hour = cale.get(Calendar.HOUR_OF_DAY);
		DateUtils.clearCale(cale);
		if(hour < startClock){//减一天
			cale.set(Calendar.HOUR_OF_DAY, startClock);
			jssj = cale.getTime();
			cale.add(Calendar.DAY_OF_YEAR, -1);
			kssj = cale.getTime();
		}else{
			cale.set(Calendar.HOUR_OF_DAY, startClock);
			kssj = cale.getTime();
			cale.add(Calendar.DAY_OF_YEAR, 1);
			jssj = cale.getTime();
		}
		Date[] dates = {kssj,jssj};
		return dates;
	}
	/*
	 * 步骤:
	 * 1 案情编号查询，此案情触动人员,并关联查询车辆派遣情况
	 * 2 若为空，则查询此中队排班人员
	 * 3 日期当前
	 * 返回值格式
	 *  sendList 派遣车辆集合
	 *  carList  消防车集合
	 *  userList 消防员列表
	 * */
	@RequestMapping("/firecaserecord/json") 
	public ModelAndView json(String ajbh,String orgId) {
		MsgBean mbean = SysFilter.getMsgBean();
		mbean.setId(0);
		if(StringUtils.isEmpty(ajbh) || StringUtils.isEmpty(ajbh)){
			mbean.setRetInfo(-1, "参数错误");
			return Json(mbean);
		}
		JSONObject resultJson = iFireCaseRecordBiz.getFireCaseRecordList(mbean,ajbh,orgId);
		if(resultJson.isEmpty()){
			//派遣车辆
			List<FireCar> sendList = new ArrayList<>();
			mbean.setId(1);//取排班日期
			Date[] dates = getDutyDate();
			//获取排班数据
			resultJson = iFireDutyBiz.getFireDutyList(orgId,dates[0],dates[1]);
			//抓取派遣车辆
			Set<String> carSet = iFireCaseInfoBiz.fetchCaseCars(ajbh);
			log.debug(carSet);
			//与派遣车辆合并
			@SuppressWarnings("unchecked")
			List<FireCar> dutycarList = (List<FireCar>)resultJson.get("carList");
			Iterator<FireCar> it = dutycarList.iterator();
			while(it.hasNext()){
				FireCar car = it.next();
				if(carSet.contains(car.getCarUuid())){
					log.debug("删除派遣车辆:" + car.getCarId() +","+ car.getCarNum());
					it.remove();
					sendList.add(car);
				}
			}
			resultJson.put("sendList", sendList);
		}
		mbean.setObj(resultJson);
		return Json(mbean);
	}
	

	@RequestMapping("/firecaserecord/saveAll") 
	public ModelAndView saveAll(String record,String ajbh,Integer orgId) {
		MsgBean mbean = SysFilter.getMsgBean(0, "保存成功 !");
		//FireUser user =getSessionUser();
		List<FireCaseRecord> recordList = JSON.parseArray(record, FireCaseRecord.class);
		iFireCaseRecordBiz.saveFireCaseRecordAll(mbean,ajbh,orgId,recordList);
		return Json(mbean);
	}
	
	@RequestMapping("/firecaserecord/boxJson") 
	public ModelAndView boxJson(FireCaseRecord fireCaseRecord) {
		return Json(iFireCaseRecordBiz.getFireCaseRecordList(fireCaseRecord));
	}

	@RequestMapping("/firecaserecord/edit") 
	public ModelAndView edit(ModelAndView mav,FireCaseRecord fireCaseRecord) {
		mav.setViewName("fire/FireCaseRecordEdit");
		if(fireCaseRecord.getRecordId() != null){
			fireCaseRecord=iFireCaseRecordBiz.getFireCaseRecord(fireCaseRecord);
		}
		mav.addObject("fireCaseRecord",fireCaseRecord);
		return mav;
	}

	@RequestMapping("/firecaserecord/save") 
	public ModelAndView save(FireCaseRecord fireCaseRecord) {
		MsgBean mbean = SysFilter.getMsgBean(0, "保存成功 !");
		iFireCaseRecordBiz.saveFireCaseRecord(mbean,fireCaseRecord);
		return Json(mbean);
	}

	@RequestMapping("/firecaserecord/del") 
	public ModelAndView del(FireCaseRecord fireCaseRecord) {
		MsgBean mbean = SysFilter.getMsgBean(0, "删除成功 !");
		iFireCaseRecordBiz.delFireCaseRecord(mbean,fireCaseRecord);
		return Json(mbean);
	}

}
